Next | Prev | Up | Top | Contents | Index

Constants With the High-Order Bit Set

A change in type sizes may yield some problems related to constants. Be careful about using constants with the high-order (sign) bit set. For instance, the hex constant 0xffffffff yields different results in the expression:

long x;
... ( (long) ( x + 0xffffffff ) ) ...
In both modes, the constant is interpreted as a 32-bit unsigned int, with value 4,294,967,295. In 32-bit mode, the addition results in a 32-bit unsigned long, which is cast to type long and has value x-1 because of the truncation to 32 bits. In 64-bit mode, the addition results in a 64-bit long with value x+4,294,967,295, and the cast is redundant.


Next | Prev | Up | Top | Contents | Index